/*
Copyright (c) 2010, Daniele Simonetti
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef PLAYLISTENTRY_H
#define PLAYLISTENTRY_H

#include <QObject>
#include <QUrl>
#include <QMetaType>

#pragma warning( push )
#pragma warning( disable : 4200 )
#include "../../deps/bassLib/c/bass.h"
#pragma warning( pop )

class PlaylistEntry : public QObject
{
    Q_OBJECT
public:
    enum TrackStatus
    {
        Playing,
        Paused,
        Stopped
    };

    PlaylistEntry(QUrl path,    bool initLater = true);
    PlaylistEntry(QString path, bool initLater = true);
    PlaylistEntry();
    PlaylistEntry(const PlaylistEntry& source);

    ~PlaylistEntry();

    bool    isNetwork   ()          const;
    double  len         ()          const;
    double  pos         ()          const;
    bool    isValid     ();

    bool    play        (bool restart = false);
    bool    pause       ();
    bool    stop        ();

    bool    setPos      (double value);

    void    setCaption  (QString caption);

    QString toString    ()          const;
    QString fullPath    ()          const;
    QString fileName    ()          const;
    QString caption     ()          const;
    QString title       ()          const;
    QString artist      ()          const;
    QString album       ()          const;
    QString genre       ()          const;
    int     track       ()          const;
    int     year        ()          const;
    int     bitrate     ()          const;
    int     channels    ()          const;
    int     samplerate  ()          const;
    int     filesize    ()          const;
    QUrl    url         ()          const;
    TrackStatus status  ()          const;
    bool    readonly    ()          const;

    void    emitEndReached();
    bool    updateTags();

signals:
    void endReached();

public slots:

protected:
    bool getTags();
    void init();
    bool createStream();

private:
    QUrl _path;
    QString _cachedtitle;
    QString _cachedartist;
    QString _cachedalbum;
    QString _cachedgenre;
    int     _cachedtrack;
    int     _cachedyear;
    int     _cachedbitrate;
    int     _cachedsamplerate;
    int     _cachedchannels;
    int     _cachedsize;
    bool    _readonly;
    TrackStatus _status;
    HSTREAM _hStream;
    mutable QString _cachedcaption;
    mutable int _cachedLen;
    mutable int _lastError;
};
Q_DECLARE_METATYPE( PlaylistEntry * );

void CALLBACK MySyncProc(HSYNC handle, DWORD channel, DWORD data, void *user);
void CALLBACK DownloadProc(void *buffer, DWORD length, void *user);


#endif // PLAYLISTENTRY_H
